package controller.link.game;

import view.client.Console;
import model.Hearer;
import java.io.Serializable;
import controller.Link;
import model.game.NPC;
import model.game.Player;
import model.User;
import model.Attendant;
import model.Server;

public class Ask4Challenge implements Link, Serializable{
    private String name;
    
    public Ask4Challenge(String name){
        this.name = name;
    }
    
    public Ask4Challenge(Hearer h, String name){
        this.name = name;
        h.send((Link)this);
    }
    
    @Override
    public void serverUse(Server server, Attendant at){
        User user = at.getUser();
        if (user.getName().equals(name)){
            NotValidPlayer nvp = new NotValidPlayer("Nome inválido");
            nvp.serverUse(server, at);
            return;
        }
        Player p = server.getWorld().getMap().findNPC(name);
        if (p!=null){
            NPC n = (NPC)p;
            ChallengeNpc cn = new ChallengeNpc(n, user.getPlayer());
            cn.serverUse(server, at);
        }
        else if (!server.testName(name)) {           
            ChallengeReceived cr = new ChallengeReceived(user.getName());
            cr.serverUse(server, server.searchAtendente(name));
            ChallengeValid cv = new ChallengeValid(name);
            cv.serverUse(server, at);
        } else {
            NotValidPlayer nvp = new NotValidPlayer("Player inexistente");
            nvp.serverUse(server, at);
        }
        at.send((Link)this);
    }

    @Override
    public void clientUse(Console console) {
        
    }

   
}
